141. 环形链表

141. 环形链表

Similar Question

leading to the advanced question

Solution Tips

方案一: 哈希表

参考 160. 相交链表

方案二: 快慢指针 判圈算法 追及问题

p9jnpm8.png

var hasCycle = function(head) {
    if (head === null) return false
    let slow = head;
    let fast = head.next;

    while (fast !== null && fast.next !== null) {
        if (slow === fast) {
            return true;
        }
        slow = slow.next;
        fast = fast.next.next;
    }

    return false;
};